package GUI;

import Logica.Pen;

import Logica.Treatment1;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JaulaMascota extends JFrame {
  Pen pen = new Pen();
  private JLabel jlabelJaula = new JLabel();
  private JButton jbuttonAgregarT = new JButton();

  private JButton jBModificarTrata = new JButton();
  Treatment1 tre = new Treatment1();
  private JTable jTableJaulas = new JTable();
  private JButton eliminarJaula = new JButton();

  public JaulaMascota() {
    try {
      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(747, 569));
    this.setForeground(Color.white);
    this.setTitle("JAULAS");
    this.setBackground(Color.white);
    jlabelJaula.setText("JAULAS");
    jlabelJaula.setBounds(new Rectangle(260, 10, 180, 55));
    jlabelJaula.setFont(new Font("ALGERIAN", 0, 22));
    jlabelJaula.setForeground(new Color(248, 14, 30));
    jbuttonAgregarT.setText("AGREGAR ");
    jbuttonAgregarT.setBounds(new Rectangle(250, 470, 150, 45));
    jbuttonAgregarT.setFont(new Font("ARIAL BLACK", 0, 14));
    jbuttonAgregarT.setForeground(new Color(0, 245, 245));
    jbuttonAgregarT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jbuttonAgregarT_actionPerformed(e);
        }
      });

    jBModificarTrata.setText("MODIFICAR");
    jBModificarTrata.setBounds(new Rectangle(410, 470, 145, 45));
    jBModificarTrata.setFont(new Font("arial black", 0, 14));
    jBModificarTrata.setForeground(new Color(0, 245, 245));


    //tabla

    jBModificarTrata.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBModificarTrata_actionPerformed(e);
        }
      });


    Object[][] info = pen.DarDatosGuiPen();


    String[] tit = { " NUMBER PEN", "CAPACITY", "STATUS", "CLINIC NUMBER" };
    DefaultTableModel dtm = new DefaultTableModel(info, tit);

    jTableJaulas = new JTable(dtm);

    jTableJaulas.setPreferredScrollableViewportSize(new Dimension(300,
                                                                  100)); // las dimensiones
    jTableJaulas.setBounds(new Rectangle(10, 100, 705, 350));
    //jScrollPane  = new JScrollPane(jTableTreatment);
    jTableJaulas.setPreferredSize(new Dimension(100, 16));
    jTableJaulas.setSize(new Dimension(300, 350));
    eliminarJaula.setText("ELIMINAR");
    eliminarJaula.setBounds(new Rectangle(565, 470, 145, 45));
    eliminarJaula.setFont(new Font("arial black", 0, 14));
    eliminarJaula.setForeground(new Color(0, 245, 245));
    eliminarJaula.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBEliminarTrata_actionPerformed(e);
        }
      });
    jTableJaulas.setBounds(10, 100, 650, 350);
    //this.getContentPane().add(jScrollPane);

    this.getContentPane().add(eliminarJaula, null);
    this.getContentPane().add(jBModificarTrata, null);
    this.getContentPane().add(jbuttonAgregarT, null);
    this.getContentPane().add(jlabelJaula, null);
    this.getContentPane().add(jTableJaulas, null);
  }

  private void jbuttonAgregarT_actionPerformed(ActionEvent e) {

    AgregarJaula a = new AgregarJaula();
    a.setVisible(true);

  }


  private void jBModificarTrata_actionPerformed(ActionEvent e) {
    int fila = jTableJaulas.getSelectedRow();
    System.out.println(fila);
    Object[] info = new Object[8];
    for (int i = 0; i < jTableJaulas.getColumnCount(); i++) {
      info[i] = jTableJaulas.getValueAt(fila, i);
    }
    ModificarJaula a = new ModificarJaula(info);
    a.setVisible(true);
  }

  private void jBEliminarTrata_actionPerformed(ActionEvent e) {

    int fila = jTableJaulas.getSelectedRow();

    Object[] info = new Object[8];
    for (int i = 0; i < jTableJaulas.getColumnCount(); i++) {
      info[i] = jTableJaulas.getValueAt(fila, i);
    }
    System.out.println(info[0]);
    if (pen.EliminarJaula(String.valueOf(info[0]))) {
      JOptionPane.showMessageDialog(null, "Eliminado",
                                    "La Jaula ha sido eliminado",
                                    JOptionPane.INFORMATION_MESSAGE);
    } else {
      JOptionPane.showMessageDialog(null, "No Eliminado",
                                    "La Jaula no ha sido Eliminado",
                                    JOptionPane.ERROR_MESSAGE);
    }


  }
}
